﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Data.Odbc;
using System.Data;

/// <summary>
/// Summary description for Mailer
/// </summary>
public class Mailer
{

    private int feedbackID;

    public Mailer(int feedback)
    {
        feedbackID = feedback;
    }

    public Mailer()
    {
    }

    public Boolean mailCreateNotification()
    {
        try
        {
            String sql = "SELECT Course_ID, Course_Name, Activation_Date, End_Date FROM course_feedback WHERE Feedback_ID = " + feedbackID;
            DataSet dataSet = new DataSet();
            DbUtilities dbUtil = new DbUtilities(sql);
            dataSet = dbUtil.retrieveInfo();
            String CourseID = (String)dataSet.Tables[0].Rows[0].ItemArray[0];
            String CourseName = (String)dataSet.Tables[0].Rows[0].ItemArray[1];
            Console.WriteLine(dataSet.Tables[0].Rows[0].ItemArray[2]);
            //int year = Int32.Parse((dataSet.Tables[0].Rows[0].ItemArray[2]).Split('-')[0]);
            DateTime Start_Date = new DateTime();
            Start_Date = (DateTime)(dataSet.Tables[0].Rows[0].ItemArray[2]);

            DateTime End_Date = new DateTime();
            End_Date = (DateTime)(dataSet.Tables[0].Rows[0].ItemArray[3]);

            MailMessage mail = new MailMessage();

            sql = "SELECT Student_Email FROM student WHERE Feedback_ID = " + feedbackID;

            dbUtil = new DbUtilities(sql);
            dataSet = dbUtil.retrieveInfo();
            for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
            {
                mail.To.Add((String)dataSet.Tables[0].Rows[i].ItemArray[0]);
            }

            mail.From = new MailAddress("feedbacksystem.iiitd@gmail.com");
            mail.Subject = "Feedback Creation Notification (" + CourseName + " " + CourseID + ")";
            string Body = "Hi,\n\nThis is to inform you that a course feedback is created for the course " + CourseName + ".";
            Body += "\nStart Date : " + Start_Date.ToLongDateString();
            Body += "\nEnd Date : " + End_Date.ToLongDateString();
            mail.Body = Body;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("feedbacksystem.iiitd", "feedback@iiitd");
            smtp.Send(mail);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    public Boolean mailReminder()
    {
        try
        {
            String sql = "SELECT Course_ID, Course_Name, Activation_Date, End_Date FROM course_feedback WHERE Feedback_ID = " + feedbackID;
            DataSet dataSet = new DataSet();
            DbUtilities dbUtil = new DbUtilities(sql);
            dataSet = dbUtil.retrieveInfo();
            String CourseID = (String)dataSet.Tables[0].Rows[0].ItemArray[0];
            String CourseName = (String)dataSet.Tables[0].Rows[0].ItemArray[1];
            Console.WriteLine(dataSet.Tables[0].Rows[0].ItemArray[2]);
            //int year = Int32.Parse((dataSet.Tables[0].Rows[0].ItemArray[2]).Split('-')[0]);
            DateTime Start_Date = new DateTime();
            Start_Date = (DateTime)(dataSet.Tables[0].Rows[0].ItemArray[2]);

            DateTime End_Date = new DateTime();
            End_Date = (DateTime)(dataSet.Tables[0].Rows[0].ItemArray[3]);

            MailMessage mail = new MailMessage();

            sql = "SELECT Student_Email FROM student WHERE Feedback_ID = " + feedbackID;

            dbUtil = new DbUtilities(sql);
            dataSet = dbUtil.retrieveInfo();
            for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
            {
                mail.To.Add((String)dataSet.Tables[0].Rows[i].ItemArray[0]);
            }

            mail.From = new MailAddress("feedbacksystem.iiitd@gmail.com");
            mail.Subject = "Reminder : Course Feedback (" + CourseName + " " + CourseID + ")";
            string Body = "Hi,\n\nThis is to remind you to fill the course feedback for the course " + CourseName + ".";
            Body += "\nStart Date : " + Start_Date.ToLongDateString();
            Body += "\nEnd Date : " + End_Date.ToLongDateString();
            mail.Body = Body;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 25;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("feedbacksystem.iiitd", "feedback@iiitd");
            smtp.Send(mail);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    public Boolean mailResult(string resultTxt, string resultPDF)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add("pratyush08040@iiitd.ac.in");
        mail.To.Add("ankit08009@iiitd.ac.in");
        mail.To.Add("vibhas@gmail.com");
        mail.To.Add("priyanshi90@gmail.com");
        mail.From = new MailAddress("feedbacksystem.iiitd@gmail.com");
        mail.Subject = "Test Email with Attachment";
        string Body = "Hey!! This is just one more test mail from my program with an attachment. Sorry once again! :D\n\n\nVibhas";
        mail.Body = Body;
        Attachment attachTxt = new Attachment(resultTxt);
        attachTxt.Name = "TestAttachment.txt";
        mail.Attachments.Add(attachTxt);
        Attachment attachPDF = new Attachment(resultPDF);
        attachPDF.Name = "TestAttachment.pdf";
        mail.Attachments.Add(attachPDF);
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.Credentials = new System.Net.NetworkCredential("feedbacksystem.iiitd", "feedback@iiitd");
        smtp.Send(mail);
        return true;
    }


    public void mailNewPassword(String emailAddress, String newPassword)
    {
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("feedbacksystem.iiitd@gmail.com");
        mail.To.Add(emailAddress);
        mail.Subject = "Online Course Feedback System : Change Password";
        string Body = "Your new Password is " + newPassword;

        mail.Body = Body;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.Credentials = new System.Net.NetworkCredential("feedbacksystem.iiitd", "feedback@iiitd");
        smtp.Send(mail);
    }




}